define(['jquery', 'bootstrap', 'backend', 'table', 'qrcode', 'form'], function ($, undefined, Backend, Table, qrcode, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('AuthName'),align:'left'},
                        {field: 'appid', title: __('app_id')},
                        {field: 'own_type', title: __('OwnType'), formatter: Controller.api.formatter.own_type},
                        {field: 'qrcode_local_text', title: __('Qrcode'), formatter: Controller.api.formatter.thumb},
                        {field: 'status', title: __('Status'), formatter: Controller.api.formatter.status},
                        {field: 'default', title: __('now_default'), align: 'center', formatter: Controller.api.formatter.default},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'updatetime', title: __('Update time'), formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'operate', title: __('Operate'),  events: Controller.api.events.operate,formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).on("click", '.own_type input', function(){
                $(".type-item").addClass('hidden');
                if($(this).val() == '2'){
                    $(".type-item.partner").removeClass('hidden');
                }
            });
        },
        pay: function (){
            Controller.api.bindevent();
            // 注意：顺序分别是目标表单、提交前置、返回后置 的回调
            Form.api.bindevent($("form[role=form]"), null, function(form, data, ret) {
                if ($('#qrcode canvas').length == 1){
                    $('#qrcode canvas').remove();
                }
                $('#qrcode').qrcode(data.data.qrcode);
                var paid;
                paid = setInterval(function() {
                    $.getJSON('/wechat/admin/auth/rechargeResult',{
                        trade_no:data.data.trade_no
                        ,_t:Date.parse(new Date())
                    },function(ret){
                        if (ret.code == 1){
                            clearInterval(paid);
                            Toastr.success(ret.msg ? ret.msg : __('Operation completed'));
                            setTimeout(function(){
                                var index = parent.Layer.getFrameIndex(window.name);
                                parent.Layer.close(index);
                                parent.location.reload();
                            }, 3000);
                        }
                    });
                },3000);
                return false;
            });
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));

            },
            formatter: {
                status: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.status == 'normal') {
                        _color = 'success';
                    }else{
                        _color = 'danger';
                    }
                    return '<span class="text-'+ _color +'">' + row.status_text + '</span>';
                },
                default: function (value, row, index, custom) {
                    return "<a href='javascript:;' data-url='admin.auth/default' class='btn btn-" + (value ? "info" : "default") + " btn-xs btn-change' data-id='" + row.id + "' data-params='" + (value ? 0 : 1) + "'>" + (value ? __('Yes') : __('No')) + "</a>";
                },
                thumb: function (value, row, index) {
                        return '<a href="' + row.qrcode_local_text + '" target="_blank"><img src="' + row.qrcode_local_text + '" alt="" style="max-height:90px;max-width:120px"></a>';
                },
                own_type: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.own_type == '1') {
                        _color = 'success';
                    }
                    return '<span class="text-'+ _color +'"> ' + row.own_type_text + '</span>';
                },
                operate: function (value, row, index) {
                    var _operate = '<a href="javascript:;" class="btn btn-info btn-xs btn-pay btn-change"><i class="fa fa-usd"></i>' + __('充值') + '</a>&nbsp;';
                    return _operate + Table.api.formatter.operate(value, row, index, $("#table"));
                }
            },
            events: {
                operate: $.extend({
                    'click .btn-pay': function (e, value, row, index) {
                        Backend.api.open('/wechat/admin/auth/pay/id/' + row['id'], __('充值'));
                    }
                }, Table.api.events.operate)
            }
        },
    };
    return Controller;
});